//
//  BaseRequest.m
//  GeneralProject
//
//  Created by zhangkai on 15/10/19.
//  Copyright © 2015年 AC. All rights reserved.
//

#import "BaseRequest.h"
#import "RequestHelper.h"
#import "MJExtension.h"

@implementation BaseRequest

+(void)sendAsyncRequestWithRequestName:(NSString *)requestName requestMethod:(NSString *)requestMethod requestModel:(BaseRequestModel *)requestModel delegate:(id)delegate method:(SEL)sel host:(NSString *)host
{
    NSString *url = [NSString stringWithFormat:@"%@%@",@"http://t-app.svc.moi.mc.cc/"/*//todo kBASEURL*/,requestName];
    if (host) {
        url = [NSString stringWithFormat:@"%@%@",host,requestName];
    }
    
    NSMutableDictionary *headerParams = [RequestHelper createRequestHeaderParams];
    NSMutableDictionary *bodyParams = [requestModel mj_keyValues];
    if (requestModel.needSetPage) {
        NSMutableDictionary *pageDict = [RequestHelper createPagingSettingWithPageIndex:requestModel.index size:requestModel.size direction:requestModel.direction orderBy:requestModel.orderBy];
        [bodyParams setObject:pageDict forKey:@"PagingSetting"];
    }
    
    //移除BaseRequestModel带的字段
    if ([bodyParams objectForKey:@"needSetPage"]) {
        [bodyParams removeObjectForKey:@"needSetPage"];
    }
    if ([bodyParams objectForKey:@"index"]) {
        [bodyParams removeObjectForKey:@"index"];
    }
    if ([bodyParams objectForKey:@"size"]) {
        [bodyParams removeObjectForKey:@"size"];
    }
    if ([bodyParams objectForKey:@"direction"]) {
        [bodyParams removeObjectForKey:@"direction"];
    }
    if ([bodyParams objectForKey:@"orderBy"]) {
        [bodyParams removeObjectForKey:@"orderBy"];
    }
    if ([bodyParams objectForKey:@"requestUrl"]) {
        [bodyParams removeObjectForKey:@"requestUrl"];
    }
    if ([bodyParams objectForKey:@"requestType"]) {
        [bodyParams removeObjectForKey:@"requestType"];
    }
    if ([bodyParams objectForKey:@"isCache"]) {
        [bodyParams removeObjectForKey:@"isCache"];
    }
    if ([bodyParams objectForKey:@"cacheType"]) {
        [bodyParams removeObjectForKey:@"cacheType"];
    }
    if ([bodyParams objectForKey:@"delegate"]) {
        [bodyParams removeObjectForKey:@"delegate"];
    }
    if ([bodyParams objectForKey:@"callBackMethod"]) {
        [bodyParams removeObjectForKey:@"callBackMethod"];
    }
    if ([bodyParams objectForKey:@"noMD5Params"]) {
        [bodyParams removeObjectForKey:@"noMD5Params"];
    }
    
    // 解决bool类型的加密问题
    if (requestModel.noMD5Params) {
        NSMutableDictionary *md5Params = [NSMutableDictionary dictionaryWithDictionary:bodyParams];
        for (NSString *keyStr in requestModel.noMD5Params) {
            [md5Params removeObjectForKey:keyStr];
        }
        
        NSString *privateStr = [RequestHelper createSiginCode:requestName withParams:md5Params withTimeStamp:[headerParams objectForKey:@"Timestamp"]];
        if (privateStr && headerParams) {
            [headerParams setObject:privateStr forKey:@"MD5"];
        }
    }
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithCapacity:1];
    if (headerParams) {
        [params setObject:headerParams forKey:@"requestHead"];
    }
    
    if (bodyParams) {
        [params setObject:bodyParams forKey:@"body"];
        
    }
    
    HFURLRequest *request = [[HFURLRequest alloc] initRequestWithUrl:url urlParams:nil method:requestMethod headerParams:nil bodyParams:params format:REQUEST_FORAMT_JSON];
    
    NSMutableDictionary *sendParams = [[NSMutableDictionary alloc] initWithCapacity:1];
    //必须传
    //RequestCallBackDispatch要根据requestName存取hander，hander根据delegate和methodname回调
    if (requestName) {
        [sendParams setObject:requestName forKey:@"requestName"];
    }
    if (requestModel) {
        [sendParams setObject:requestModel forKey:@"requestModel"];
    }
    if (delegate) {
        [sendParams setObject:delegate forKey:@"delegate"];
    }
    if (sel) {
        [sendParams setObject:NSStringFromSelector(sel) forKey:@"methodName"];
    }
    
    [[HFHttpProtocol defaultRequest] sendAsyncRequest:request reqParams:sendParams className:NSStringFromClass([self class])];
    
}
+ (void)requestResultCallBack:(NSDictionary *)obj{
    NSMutableDictionary *retDic = [RequestHelper resovleRequestCallBack:obj];
    NSDictionary *sendParams = [obj objectForKey:@"seq"];
    if (sendParams) {
        if ([sendParams objectForKey:@"requestModel"]) {
            [retDic setObject:[sendParams objectForKey:@"requestModel"] forKey:@"requestModel"];
        }
        if ([sendParams objectForKey:@"delegate"]) {
            [retDic setObject:[sendParams objectForKey:@"delegate"] forKey:@"delegate"];
        }
        if ([sendParams objectForKey:@"methodName"]) {
            [retDic setObject:[sendParams objectForKey:@"methodName"] forKey:@"methodName"];
        }
        NSString *requestName = [sendParams objectForKey:@"requestName"];
        if (requestName) {
            [[RequestCallBackDispatch shareRequestDispatch] callbackWithRequestName:requestName returnObject:retDic];
        }
        
    }
}


@end
